package com.technology.education.ui.student.home

import android.os.Bundle
import com.technology.education.R
import com.technology.education.data.model.Carousel
import com.technology.education.databinding.FragmentHomeStudentBinding
import com.technology.education.ext.checkLogin
import com.technology.education.ext.customDialog
import com.technology.education.ext.getAppViewModel
import com.technology.education.ext.navigation
import com.technology.education.ui.common.web.WebActivity
import com.technology.education.widget.HomeBannerAdapter
import com.youth.banner.Banner
import com.youth.banner.indicator.CircleIndicator
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment
import me.hgj.jetpackmvvm.util.startActivity

/**
 * 学生首页
 */
class HomeFragment : BaseVmDbFragment<HomeViewModel, FragmentHomeStudentBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        mDatabind.ivSearch.setOnClickListener {
            navigation(it, R.id.action_main_fragment_to_searchFragment)
        }

        mDatabind.llInfo.btnConfirm.setOnClickListener {
            if (checkLogin()) {
                navigation(it, R.id.action_mine_to_publishFragment)
            }
        }

        mDatabind.imgBanner.setOnClickListener {
            navigation(requireView(), R.id.action_main_fragment_to_courseCategoryFragment)
        }
    }

    override fun lazyLoadData() {
        setBanner(arrayListOf())
    }

    override fun createObserver() {
        getAppViewModel().city.observe(this) {
            mViewModel.status.set(it)
        }
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    /**
     * 填充banner 数据
     */
    private fun setBanner(listBanner: ArrayList<Carousel>) {
        (mDatabind.banner as Banner<Carousel, *>).apply {
            addBannerLifecycleObserver(viewLifecycleOwner)
            adapter = HomeBannerAdapter(listBanner)
            setOnBannerListener { data, _ ->
                if (data is Carousel && !data.url.isNullOrEmpty()) {
                    startActivity(WebActivity::class, Pair("title", "资讯"), Pair("url", data.url))
                }
//                navigation(requireView(), R.id.action_main_fragment_to_courseCategoryFragment)
            }
            setIndicator(CircleIndicator(context))
        }
    }
}